package com.smartleanx.module.hrm.service.esign.service.impl;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartleanx.module.hrm.dal.mysql.esign.HrmEFolderTempMapper;
import com.smartleanx.module.hrm.service.esign.service.HrmEFolderService;
import com.smartleanx.module.hrm.service.esign.service.HrmEFolderTempService;
import com.smartleanx.module.hrm.service.esign.temp.po.HrmEFolder;
import com.smartleanx.module.hrm.service.esign.temp.po.HrmEFolderTemp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * e签宝文件夹存放的模板Service实现
 *
 * @author xzq
 * @since 2025-07-07 16:46:29
 */
@Service
public class HrmEFolderTempServiceImpl extends ServiceImpl<HrmEFolderTempMapper, HrmEFolderTemp> implements HrmEFolderTempService {
    @Autowired
    private HrmEFolderService hrmEFolderService;

    @Override
    public int saveList(List<Long> folderIds, Long tempId) {
        List<HrmEFolderTemp> collect = folderIds.stream().map(p -> {
            HrmEFolderTemp hrmEFolderTemp = new HrmEFolderTemp();
            hrmEFolderTemp.setFolderId(p);
            hrmEFolderTemp.setTempId(tempId);
            hrmEFolderTemp.setDataStatus(1);
            return hrmEFolderTemp;
        }).collect(Collectors.toList());

        saveBatch(collect);

        return collect.size();
    }

    @Override
    public List<Long> getFolderIdsByTempId(Long tempId,String folderCode) {
        List<Long> folderIds = getFolderTempListByTempId(tempId).stream().map(HrmEFolderTemp::getFolderId).collect(Collectors.toList());
        List<HrmEFolder> hrmEFolderList = hrmEFolderService.getFolderByIds(folderIds);
        if(StrUtil.isNotBlank(folderCode)){
            hrmEFolderList = hrmEFolderList.stream().filter(p ->p.getFolderType().equals(folderCode)).collect(Collectors.toList());
        }
        return hrmEFolderList.stream().map(HrmEFolder::getId).collect(Collectors.toList());
    }

    @Override
    public List<HrmEFolderTemp> getFolderTempListByTempId(Long tempId) {
        return list(new LambdaQueryWrapper<HrmEFolderTemp>()
        .eq(HrmEFolderTemp::getDataStatus,1)
        .eq(HrmEFolderTemp::getTempId,tempId));
    }

    @Override
    public List<HrmEFolder> getFolderListByTempId(Long tempId) {
        List<Long> folderIds = getFolderTempListByTempId(tempId).stream().map(HrmEFolderTemp::getFolderId).collect(Collectors.toList());
        return hrmEFolderService.getFolderByIds(folderIds);
    }

    @Override
    public List<HrmEFolderTemp> getFolderListByFolderId(Long folderId,List<Long> tempIds) {
        List<HrmEFolder> parentList = hrmEFolderService.getFolderListByFolderId(folderId);
        List<Long> ids = new ArrayList<>();
        if(CollUtil.isNotEmpty(parentList)){
            ids = parentList.stream().map(HrmEFolder::getId).collect(Collectors.toList());
        }
        if(ids == null){
            ids = new ArrayList<>();
        }
        ids.add(folderId);
        return list(new LambdaQueryWrapper<HrmEFolderTemp>()
        .eq(HrmEFolderTemp::getDataStatus,1)
        .in(HrmEFolderTemp::getFolderId,ids)
        .in(HrmEFolderTemp::getTempId,tempIds));
    }

    @Override
    public List<HrmEFolderTemp> getFolderListByFolderId(Long folderId) {
        return list(new LambdaQueryWrapper<HrmEFolderTemp>()
                .eq(HrmEFolderTemp::getDataStatus,1)
                .eq(HrmEFolderTemp::getFolderId,folderId));
    }
}
